
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       sensor_rf_backend_serial.h
  * @author     baiyang
  * @date       2023-2-9
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include "sensor_rf_backend.h"
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
typedef struct sensor_rf_backend_ser* sensor_rf_backend_ser_t;
typedef const struct sensor_rf_backend_ser* sensor_rf_backend_ser_const_t;
typedef struct sensor_rf_backend_ser_ops* sensor_rf_backend_ser_ops_t;

struct sensor_rf_backend_ser {
    struct sensor_rf_backend backend;

    sensor_rf_backend_ser_ops_t ops_ser;
    rt_device_t              uart;
};

struct sensor_rf_backend_ser_ops {
    // baudrate used during object construction:
    uint32_t (*initial_baudrate)(sensor_rf_backend_const_t rf_backend, uint8_t serial_instance);

    // the value 0 is special to the UARTDriver - it's "use default"
    uint16_t (*rx_bufsize)(sensor_rf_backend_const_t rf_backend);
    uint16_t (*tx_bufsize)(sensor_rf_backend_const_t rf_backend);

    // it is essential that anyone relying on the base-class update to
    // implement this:
    bool (*get_reading)(sensor_rf_backend_t rf_backend, float *reading_m);

    // maximum time between readings before we change state to NoData:
    uint16_t (*read_timeout_ms)(sensor_rf_backend_const_t rf_backend);
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
void sensor_rf_backend_serial_ctor(sensor_rf_backend_ser_t rf_backend_ser,
                                    sensor_rf_backend_ser_ops_t _ops_ser,
                                    sensor_rf_backend_ops_t _ops,
                                    sensor_rf_state_t _state, sensor_rf_params_t _params,
                                    const char *name);

void sensor_rf_bind_backend_function(sensor_rf_backend_ops_t ops_frontend, sensor_rf_backend_ops_t ops_backend);

/// 
uint32_t sensor_rf_InitialBaudrate(sensor_rf_backend_const_t rf_backend, uint8_t serial_instance);

/// 
// baudrate used during object construction:
static inline uint32_t sensor_rf_initial_baudrate(sensor_rf_backend_const_t rf_backend, uint8_t serial_instance) {
    if (((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->initial_baudrate != NULL) {
        return ((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->initial_baudrate(rf_backend, serial_instance);
    }

    return sensor_rf_InitialBaudrate(rf_backend, serial_instance);
}

static inline uint16_t sensor_rf_rx_bufsize(sensor_rf_backend_const_t rf_backend) {
    if (((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->rx_bufsize != NULL) {
        return ((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->rx_bufsize(rf_backend);
    }

    return 0;
}

static inline uint16_t sensor_rf_tx_bufsize(sensor_rf_backend_const_t rf_backend) {
    if (((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->tx_bufsize != NULL) {
        return ((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->tx_bufsize(rf_backend);
    }

    return 0;
}

static inline uint16_t sensor_rf_get_reading(sensor_rf_backend_t rf_backend, float *reading_m) {
    RT_ASSERT(((sensor_rf_backend_ser_t)rf_backend)->ops_ser->get_reading != NULL);
    return ((sensor_rf_backend_ser_t)rf_backend)->ops_ser->get_reading(rf_backend, reading_m);
}

static inline uint16_t sensor_rf_read_timeout_ms(sensor_rf_backend_const_t rf_backend) {
    if (((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->read_timeout_ms != NULL) {
        return ((sensor_rf_backend_ser_const_t)rf_backend)->ops_ser->read_timeout_ms(rf_backend);
    }

    return 200;
}
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



